package com.example.managemyself.data.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.managemyself.data.model.UserApiKey;

import java.util.List;

/**
 * 用户API密钥数据访问对象
 */
@Dao
public interface UserApiKeyDao {
    
    /**
     * 插入API密钥
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(UserApiKey userApiKey);
    
    /**
     * 更新API密钥
     */
    @Update
    void update(UserApiKey userApiKey);
    
    /**
     * 删除API密钥
     */
    @Delete
    void delete(UserApiKey userApiKey);
    
    /**
     * 根据ID删除API密钥
     */
    @Query("DELETE FROM user_api_keys WHERE id = :id")
    void deleteById(int id);
    
    /**
     * 获取用户的所有API密钥
     */
    @Query("SELECT * FROM user_api_keys WHERE userId = :userId ORDER BY createdDate DESC")
    LiveData<List<UserApiKey>> getUserApiKeys(String userId);
    
    /**
     * 获取用户特定类型的API密钥
     */
    @Query("SELECT * FROM user_api_keys WHERE userId = :userId AND apiType = :apiType AND isActive = 1 LIMIT 1")
    LiveData<UserApiKey> getUserApiKeyByType(String userId, String apiType);
    
    /**
     * 同步获取用户特定类型的API密钥
     */
    @Query("SELECT * FROM user_api_keys WHERE userId = :userId AND apiType = :apiType AND isActive = 1 LIMIT 1")
    UserApiKey getUserApiKeyByTypeSync(String userId, String apiType);
    
    /**
     * 获取用户的DeepSeek API密钥
     */
    @Query("SELECT * FROM user_api_keys WHERE userId = :userId AND apiType = 'deepseek' AND isActive = 1 LIMIT 1")
    LiveData<UserApiKey> getUserDeepSeekKey(String userId);
    
    /**
     * 同步获取用户的DeepSeek API密钥
     */
    @Query("SELECT * FROM user_api_keys WHERE userId = :userId AND apiType = 'deepseek' AND isActive = 1 LIMIT 1")
    UserApiKey getUserDeepSeekKeySync(String userId);
    
    /**
     * 检查用户是否已有特定类型的API密钥
     */
    @Query("SELECT COUNT(*) FROM user_api_keys WHERE userId = :userId AND apiType = :apiType")
    int hasApiKeyOfType(String userId, String apiType);
    
    /**
     * 停用用户某类型的所有API密钥
     */
    @Query("UPDATE user_api_keys SET isActive = 0 WHERE userId = :userId AND apiType = :apiType")
    void deactivateApiKeysByType(String userId, String apiType);
    
    /**
     * 获取所有激活的API密钥
     */
    @Query("SELECT * FROM user_api_keys WHERE userId = :userId AND isActive = 1 ORDER BY createdDate DESC")
    LiveData<List<UserApiKey>> getActiveApiKeys(String userId);
    
    /**
     * 根据ID获取API密钥
     */
    @Query("SELECT * FROM user_api_keys WHERE id = :id")
    LiveData<UserApiKey> getApiKeyById(int id);
    
    /**
     * 删除用户的所有API密钥
     */
    @Query("DELETE FROM user_api_keys WHERE userId = :userId")
    void deleteAllUserApiKeys(String userId);
    
    /**
     * 更新API密钥的激活状态
     */
    @Query("UPDATE user_api_keys SET isActive = :isActive WHERE id = :id")
    void updateActiveStatus(int id, boolean isActive);
    
    /**
     * 批量插入API密钥
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<UserApiKey> userApiKeys);
} 